package com.eckey.lab.controller;

import com.alibaba.fastjson.JSON;
import com.eckey.lab.entity.Storage;
import com.eckey.lab.service.StorageService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * (Storage)表控制层
 *
 * @author makejava
 * @since 2023-09-24 15:44:08
 */
@RestController
@RequestMapping("storage")
public class StorageController {
    /**
     * 服务对象
     */
    @Resource
    private StorageService storageService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Storage> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.storageService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param storage 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Storage> add(Storage storage) {
        return ResponseEntity.ok(this.storageService.insert(storage));
    }

    /**
     * 编辑数据
     *
     * @param storage 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Storage> edit(Storage storage) {
        return ResponseEntity.ok(this.storageService.update(storage));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.storageService.deleteById(id));
    }


    @PostMapping("/deduct")
    public String deduct(@RequestParam("name") String productName, @RequestParam("totalPrice") Integer totalPrice) {
        HashMap<String, Object> maps = new HashMap<String, Object>();
        storageService.deduct(productName, totalPrice);
        maps.put("code", "0");
        maps.put("msg", "success");
        return JSON.toJSONString(maps);
    }

}

